import { _decorator, Color, Component, instantiate, Label, Node, Prefab, Sprite, SpriteFrame } from 'cc';
import { Village } from '../entity/Village';
import { UIManager } from '../common/UIManager';
import { PrefabEnum } from '../common/PrefabEnum';
import { EventManager } from '../event/EventManager';
import { Army, ArmyType, SpyType } from '../entity/Army';
import { CatapultItem } from './CatapultItem';
import { Building } from '../entity/Buildings';
import ArmyApi from '../api/ArmyApi';
import { Utils } from '../common/Utils';
import WorldManager from '../common/WorldManager';
import { UserInfo } from '../common/UserInfo';
import { WorldController } from '../common/WorldController';
const { ccclass, property } = _decorator;

@ccclass('ComfirmArmy')
export class ComfirmArmy extends Component {
    @property(Label)
    villageName:Label
   
    @property(Label)
    player:Label
    @property(Label)
    time:Label
    @property(Node)
    submit:Node
    @property(Node)
    close:Node
    @property(Node)
    cancel:Node
    @property(Node)
    spyLayout:Node
    @property(Node)
    economyBtn:Node
    @property(Node)
    armyBtn:Node
    @property(Node)
    catapultLayout:Node
    @property(Prefab)
    catapultItem:Prefab
   

    @property(SpriteFrame)
    circle1:SpriteFrame
    @property(SpriteFrame)
    circle2:SpriteFrame

     _army:Army
     _village:Village
     _catapultItemNodeList:Node[]
    start(){
        this.submit.on(Node.EventType.TOUCH_END, this.onSubmit, this)
        this.close.on(Node.EventType.TOUCH_END, this.onClose, this)
        this.cancel.on(Node.EventType.TOUCH_END, this.onClose, this)

        this.economyBtn.on(Node.EventType.TOUCH_END, this.onEconomy, this)
        this.armyBtn.on(Node.EventType.TOUCH_END, this.onArmy, this)
    }

    setData(village:Village, army:Army) {
        this._village = village
        this._army = army

        this.villageName.string = `${village.name} (${village.x} | ${village.y})` 
       
        this.player.string = village.user.uInfo.name
        if(!village.user.uInfo.name){
            this.player.node.parent.active = false
        }

        
        this.time.string = Utils.secondToStr(army.moveTime(UserInfo.instance.villageData.zoneId,village.zoneId, army.speed, army.speed))

        console.log('army:', army)
        // 判断是否是侦察类型
        if(army.armyType == ArmyType.Detection){
            this.spyLayout.active = true
            this.onEconomy()
        } else {
            this.spyLayout.active = false
        }

        // 判断投石车的数量
       
        let nodeCount = 0
        if(0 < army.catapultCount && army.catapultCount <= 20){
            nodeCount = 1
        } 
        if( army.catapultCount > 20) {
            nodeCount = 2
        }
        

        if(nodeCount == 0){
            this.catapultLayout.active = false
        } else{
            this.catapultLayout.active = true
        }

        this._catapultItemNodeList = []
        for(let i = 0; i< nodeCount; i++){
            let node = instantiate(this.catapultItem)
            this.catapultLayout.addChild(node)
            let b = Building.createByIdAndLevel(0, 1)
            b.id = 99
            b.name = '随机'
            node.getComponent(CatapultItem).setData(b)
            this._catapultItemNodeList.push(node)
        }


        if(army.armyType != ArmyType.Attack){
            this.catapultLayout.active = false
            this._catapultItemNodeList = []
        }
    }

    async onSubmit(){
        let params = {
            x: this._village.x,
            y: this._village.y,
            kid:this._village.zoneId,
            ...this._army.params,

        }


        let children =  this._catapultItemNodeList
        if(children[0]){
            params.kata = children[0].getComponent(CatapultItem)._building.id
        }
        if(children[1]){
            params.kata2 = children[1].getComponent(CatapultItem)._building.id
        }

        console.log(params)
        
        await UIManager.instance.showLoading()
        let res = await ArmyApi.a2bReady(params)
        console.log('出征：')
        console.log(params)
        console.log(res)
        UIManager.instance.closeLoading()
        UIManager.instance.tip('军队已出发')

        // 刷新世界地图上的行军路线
        if(WorldController.instance){
            UserInfo.instance.refreshVillageDetail()
           
            UIManager.instance.clear()
        }

        EventManager.instance.emit(EventManager.EventType.ArmySetOff,1)
        this.onClose()
    }

    onClose(){
        UIManager.instance.closeUI(PrefabEnum.ComfirmArmy)
    }

    onEconomy(){
        this._army.spyType = SpyType.Economy

       

        this.economyBtn.getChildByName('Circle').getComponent(Sprite).spriteFrame = this.circle2
        this.armyBtn.getChildByName('Circle').getComponent(Sprite).spriteFrame = this.circle1

        this.economyBtn.getChildByName('Label').getComponent(Label).color = new Color('#FFFFFF')
        this.armyBtn.getChildByName('Label').getComponent(Label).color = new Color('#DACAB3')
    }

    onArmy(){
        this._army.spyType = SpyType.Army

        

        this.economyBtn.getChildByName('Circle').getComponent(Sprite).spriteFrame = this.circle1
        this.armyBtn.getChildByName('Circle').getComponent(Sprite).spriteFrame = this.circle2

        this.economyBtn.getChildByName('Label').getComponent(Label).color = new Color('#DACAB3')
        this.armyBtn.getChildByName('Label').getComponent(Label).color =  new Color('#FFFFFF')
    }

    
}


